package com.grantha.controllers;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.sql.*;

import org.apache.log4j.Logger;

import com.grantha.util.DB;
import com.grantha.util.Util;

/**
 * Servlet implementation class BookIssueServlet
 */
@WebServlet("/BookIssueServlet")
public class BookIssueServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	Logger log = Logger.getLogger(this.getClass());
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
				
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		
		log.info("Servlet Stated");
		//coding start 
		String bookno = request.getParameter("bookno");
		String custno = request.getParameter("custno"); 
		
		
		log.info("custname="+custno);
		log.info("bookname="+bookno);
		
		//log.info("date="+rs);
		
		
		if(!custno.equals("")){
			try {
				
				
				
				DB.update("insert into dailyissueregister (bookno,patno,dateofissue,returned) values (?, ?,curdate(),'false')", bookno,custno );
				out.print(Util.sendJSONMessage(true, "Book Issued successfully"));
					log.info("successly issued");
				} catch(Exception e) {
					out.print(Util.sendJSONMessage(false, "Error ,can't issue the book"));
					log.info("serror");
		        }
		}
		
		else{
			try{
				log.info("depositing");
				DB.update("update dailyissueregister set dateofreturn=curdate() where (bookno=? and returned='false')",bookno);
				//DB.search("select DATEDIFF(dd,dateofreturn,dateofissue) as datecount from dailyissuregister where (bookno=? and returned='false')",bookno);
				DB.update("update dailyissueregister set returned='true' where (bookno=? and returned='false')",bookno);
				out.print(Util.sendJSONMessage(true, "Book deposited successfully"));
				
			}catch(Exception e){
				System.err.print("Sorry");
			}
			//try{
				//DB.search("SELECT  DATEDIFF(dateofreturn,dateofissue) as datecount from dailyissueregister where (bookno=? and returned='false')",bookno);
						
			//}catch(Exception e){
				//System.err.print("Sorry");
			
			//}
			//try{
				//DB.update("update dailyissueregister set returned='true' where (bookno=? and returned='false')",bookno);
				
			//}catch(Exception e){
				//System.err.print("Sorry");
				
			}
		out.close();
		log.info("Servlet end");
		}
		//coding ends
		
		
	
}
